package com.zzh.takeoutapp.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.zzh.takeoutapp.R;
import com.zzh.takeoutapp.adapter.AddressAdapter;
import com.zzh.takeoutapp.adapter.OnRecyleItemClick;
import com.zzh.takeoutapp.base.BaseActivity;
import com.zzh.takeoutapp.event.NetMsgEvent;
import com.zzh.takeoutapp.model.AddressBean;
import com.zzh.takeoutapp.service.EventbusHelper;
import com.zzh.takeoutapp.service.ServiceUrl;
import com.zzh.takeoutapp.utils.DensityUtil;
import com.zzh.takeoutapp.utils.DividerGridItemDecoration;
import com.zzh.takeoutapp.utils.GsonResolve;
import com.zzh.takeoutapp.utils.LoadingUtil;
import com.zzh.takeoutapp.utils.ToolBarHelper;
import com.zzh.takeoutapp.utils.UtilTool;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/7/31.
 */

public class AddressActivity extends BaseActivity implements AddressAdapter.IonSlidingViewClickListener {
    @Bind(R.id.toolbar)
    Toolbar tbToolbar;
    @Bind(R.id.tv_add_address)
    TextView tvAddAddress;
    @Bind(R.id.recyclerView)
    RecyclerView mRecyclerView;
    private AddressAdapter addressAdapter;
    private final String ADDRESS = "AddressActivity";
    private final String DELETE_ADDRESS = "DeleteAddressActivity";
    private List<AddressBean> beanList;
    private LoadingUtil loadingUtil;

    @Override
    public void setContent() {
        setContentView(R.layout.activity_mine_address);
        ButterKnife.bind(this);
        beanList = new ArrayList<>();
        loadingUtil = new LoadingUtil(this);
        loadingUtil.startShowLoading();
        ServiceUrl.AddressServiceUrl(this, ADDRESS);
    }

    @Override
    public void initComponent() {
        ToolBarHelper.setTitleAndBackAndText(this, tbToolbar, "我的收货地址");
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));//很重要，不然不显示就是一个坑
        mRecyclerView.addItemDecoration(new DividerGridItemDecoration(this, DensityUtil.px2dip(this, 1), R.color.black));
    }

    @Override
    public void setListener() {

    }

    @Override
    public void onAfterInit() {

    }

    @Override
    public void onStart() {
        super.onStart();
        EventbusHelper.register(this);
    }

    @Override
    public void onStop() {
        loadingUtil.stopShowLoading();
        EventbusHelper.unregister(this);
        super.onStop();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMsgEvent(NetMsgEvent event) {
        String _msgType = (String) event.getMsgType();
        JSONObject jsonObject = event.getMsgJSONObj();
        switch (_msgType) {
            case ADDRESS:
                loadingUtil.stopShowLoading();
                if (jsonObject != null)
                    if (jsonObject.optString("error").equals("0")) {
                        JSONObject data = jsonObject.optJSONObject("data");
                        JSONArray items = data.optJSONArray("items");
                        if (beanList != null) {
                            beanList.removeAll(beanList);
                        }
                        for (int i = 0; i < items.length(); i++) {
                            JSONObject jsonObject1 = items.optJSONObject(i);
                            AddressBean addressBean = GsonResolve.jsonString2Bean(jsonObject1.toString(), AddressBean.class);
                            beanList.add(addressBean);
                        }
                        addressAdapter = new AddressAdapter(beanList, this);
                        mRecyclerView.setAdapter(addressAdapter);
                        addressAdapter.notifyDataSetChanged();
                        addressAdapter.setmIDeleteBtnClickListener(this);
                    }
                break;
            case DELETE_ADDRESS:
                loadingUtil.stopShowLoading();
                if (jsonObject != null)
                    if (jsonObject.optString("error").equals("0")) {
                        ServiceUrl.AddressServiceUrl(this, ADDRESS);
                    }
                break;
        }
    }


    @OnClick({R.id.tv_add_address})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.tv_add_address:
                Intent intent = new Intent(this, AddAddressActivity.class);
                intent.putExtra("add_address", "add_address");
                start_Activity(intent);
                break;
        }
    }

    @Override
    public void onItemClick(View view, int position) {
        if (getIntent().getType() != null && getIntent().getType().equals("submit_order")) {
            Intent intent = new Intent();
            intent.putExtra("addressBean", beanList.get(position));
            setResult(1, intent);
            finish_Activity();
        } else {
            Intent intent = new Intent(AddressActivity.this, AddAddressActivity.class);
            intent.putExtra("addressBean", beanList.get(position));
            start_Activity(intent);
        }
    }

    @Override
    public void onDeleteBtnCilck(View view, int position) {
        if (!loadingUtil.isShowLoading()) {
            loadingUtil.startShowLoading();
            ServiceUrl.AddrDeleteServiceUrl(this, beanList.get(position).getAddr_id(), DELETE_ADDRESS);
        }
    }
}
